package ypmits.pixelbender {
	import ypmits.components.modal.Warning;

	import flash.display.Shader;
	import flash.display.ShaderParameter;
	import flash.events.EventDispatcher;
	import flash.filters.ShaderFilter;
	import flash.utils.ByteArray;

	/**
	 * @author Dave Lenz
	 */
	public class PBJFilter extends EventDispatcher {

		private var flesh : Array;
		
		protected var _shader : Shader;
		protected var _shaderfilter : ShaderFilter;
		protected var _allsliders : Array;
		protected var _sliders : Array;

		public var filename : String;

		
		public function PBJFilter( shader : *, extraflesh : Array = null, shaderprecision : String = "fast" ) {
			shader is Class ? _shader = new Shader( new shader as ByteArray ) : _shader = shader;
			_shader.precisionHint = shaderprecision;
			_shaderfilter = new ShaderFilter( _shader );
			
			if( extraflesh ) {
				extraflesh[0] ? _shaderfilter.topExtension = extraflesh[0] : _shaderfilter.topExtension = 0;
				extraflesh[1] ? _shaderfilter.rightExtension = extraflesh[1] : _shaderfilter.rightExtension = 0;
				extraflesh[2] ? _shaderfilter.bottomExtension = extraflesh[2] : _shaderfilter.bottomExtension = 0;
				extraflesh[3] ? _shaderfilter.leftExtension = extraflesh[3] : _shaderfilter.leftExtension = 0;
			}
			
		}

		
		public function setExtraFlesh( flesh : Array ) : void {
			try {
				_shaderfilter.topExtension = flesh[0];
				_shaderfilter.rightExtension = flesh[1];
				_shaderfilter.bottomExtension = flesh[2];
				_shaderfilter.leftExtension = flesh[3];
				this.flesh = flesh;
			} catch( e : RangeError ) {
				Warning.instance.head = "RangeError!";
				Warning.instance.body = "bla e.message";
				Warning.instance.show( );
			}
		}

		
		public function setParameter( parameter : String, value : * ) : void {
			if( ( value is Number ) || ( value is int ) ) {
				ShaderParameter( _shader.data[parameter] ).value = [ value ];
			} else if ( value is Array ) {
				ShaderParameter( _shader.data[parameter] ).value = [ value[0], value[1] ];
			}
		}

		
		public function get shaderfilter() : ShaderFilter {
			return _shaderfilter;
		}
//
//		
//		protected function changeHandler( e : SliderEvent ) : void {
//			trace( "PBJFilter.changeHandler("+[e.value.toString( )]+")" );
//			e.slider.text = e.value.toString( );
//			
//			dispatchEvent( new Event( Event.CHANGE, true ) );
//		}
	}
}
